package com.monkey.luffer.springboot.demo.basis.kcc.test.lianji.majiang;


import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author jiahong.liu
 */
public class Server {
    public static void main(String[] args) throws Exception {
        server();
    }

    public static void server () throws Exception {
        ServerSocket ss = null;
        Socket s = null;
        Scheduler scheduler = new Scheduler(3);
        try{
            // 1. 创建ServerSocket类型的对象并提供端口号
            ss = new ServerSocket(9999);
            // 2. 等待客户端的连接请求，调用accept方法
            // 允许多个用户请求连接。
            int i = 0;
            while (true) {
                System.out.println("等待客户端的连接请求...");
                s = ss.accept();

                if(scheduler.getSockets().size() > 3 || scheduler.isBegin()) {
                    continue;
                }

                scheduler.addPlayer(s);
            }

        } catch(IOException e) {
            e.printStackTrace();
        } finally

        {
            // 关闭流
            try {
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

